/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Yunzi
 */
@Entity
@Table(name = "imenu_course")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "ImenuCourse.findAll", query = "SELECT i FROM ImenuCourse i"),
    @NamedQuery(name = "ImenuCourse.findById", query = "SELECT i FROM ImenuCourse i WHERE i.id = :id"),
    @NamedQuery(name = "ImenuCourse.findByName", query = "SELECT i FROM ImenuCourse i WHERE i.name = :name"),
    @NamedQuery(name = "ImenuCourse.findByPrice", query = "SELECT i FROM ImenuCourse i WHERE i.price = :price"),
    @NamedQuery(name = "ImenuCourse.findByStar", query = "SELECT i FROM ImenuCourse i WHERE i.star = :star"),
    @NamedQuery(name = "ImenuCourse.findByRecipe", query = "SELECT i FROM ImenuCourse i WHERE i.recipe = :recipe"),
    @NamedQuery(name = "ImenuCourse.findByMeasure", query = "SELECT i FROM ImenuCourse i WHERE i.measure = :measure"),
    @NamedQuery(name = "ImenuCourse.findByAvailable", query = "SELECT i FROM ImenuCourse i WHERE i.available = :available"),
    @NamedQuery(name = "ImenuCourse.findByUpdateTime", query = "SELECT i FROM ImenuCourse i WHERE i.updateTime = :updateTime"),
    @NamedQuery(name = "ImenuCourse.findByPortraitImage", query = "SELECT i FROM ImenuCourse i WHERE i.portraitImage = :portraitImage"),
    @NamedQuery(name = "ImenuCourse.findByLandscapeImage", query = "SELECT i FROM ImenuCourse i WHERE i.landscapeImage = :landscapeImage"),
    @NamedQuery(name = "ImenuCourse.findByCreateTime", query = "SELECT i FROM ImenuCourse i WHERE i.createTime = :createTime"),
    @NamedQuery(name = "ImenuCourse.countByMenu", query = "SELECT count(i) FROM ImenuCourse i WHERE i.menu = :menu"),
    @NamedQuery(name = "ImenuCourse.findByMenu", query = "SELECT i FROM ImenuCourse i WHERE i.menu = :menu"),
    @NamedQuery(name = "ImenuCourse.findByDeleted", query = "SELECT i FROM ImenuCourse i WHERE i.deleted = :deleted")})
public class ImenuCourse implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", insertable = false)
    private Integer id;
    @Size(max = 40)
    @Column(name = "name")
    private String name;
    @Column(name = "price")
    private Integer price;
    @Column(name = "star")
    private Integer star;
    @Size(max = 45)
    @Column(name = "recipe")
    private String recipe;
    @Column(name = "measure")
    private Integer measure;
    @Column(name = "available")
    private Boolean available;
    @Basic(optional = false)
    @Column(name = "update_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    @Size(max = 100)
    @Column(name = "portrait_image")
    private String portraitImage;
    @Size(max = 100)
    @Column(name = "landscape_image")
    private String landscapeImage;
    @Column(name = "create_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @Column(name = "deleted")
    private Boolean deleted;
    @JoinColumn(name = "tag_id", referencedColumnName = "id")
    @ManyToOne
    private ImenuTag tag;
    
    @Transient
    private Integer tagId;
    
    @JoinColumn(name = "menu_id", referencedColumnName = "id")
    @ManyToOne(optional = false, cascade={CascadeType.MERGE})
    private ImenuMenu menu;

    public ImenuCourse() {
    }

    public ImenuCourse(Integer id) {
        this.id = id;
    }

    public ImenuCourse(Integer id, Date updateTime) {
        this.id = id;
        this.updateTime = updateTime;
    }

    public boolean isHasLandscapeImage() {
        return this.landscapeImage != null && this.landscapeImage.length() > 1;
    }

    public boolean isHasPortraitImage() {
        return this.portraitImage != null && this.portraitImage.length() > 1;
    }

    public String getSmallLandscape() {
        System.out.println("ImenuCourse.getSmallLandscape() 1 this.landscapeImage = " +this.landscapeImage);
        if (this.isHasLandscapeImage()) {
            System.out.println("ImenuCourse.getSmallLandscape() 2");
            //return "course_" + id + "_landscape_100x75" + this.getLandscapeImage().substring(this.getLandscapeImage().lastIndexOf("."));
            return "100x75_"+this.getLandscapeImage();
        } else {
            System.out.println("ImenuCourse.getSmallLandscape() 3");
            return "../landscape.png";
        }
    }

    public String getSmallPortrait() {
        if (this.isHasPortraitImage()) {
            return "75x100_"+this.getPortraitImage();
        } else {
            return "../portrait.png";
        }
    }
    public String getMiddleLandscape() {
        System.out.println("ImenuCourse.getSmallLandscape() 1 this.landscapeImage = " +this.landscapeImage);
        if (this.isHasLandscapeImage()) {
            System.out.println("ImenuCourse.getSmallLandscape() 2");
            return "300x225_" + this.getLandscapeImage();
        } else {
            System.out.println("ImenuCourse.getSmallLandscape() 3");
            return null;
        }
    }
    public String getBigLandscape() {
        System.out.println("ImenuCourse.getSmallLandscape() 1 this.landscapeImage = " +this.landscapeImage);
        if (this.isHasLandscapeImage()) {
            System.out.println("ImenuCourse.getSmallLandscape() 2");
            return "440x330_" + this.getLandscapeImage();
        } else {
            System.out.println("ImenuCourse.getSmallLandscape() 3");
            return null;
        }
    }

    public String getHugeLandscape() {
        System.out.println("ImenuCourse.getSmallLandscape() 1 this.landscapeImage = " +this.landscapeImage);
        if (this.isHasLandscapeImage()) {
            System.out.println("ImenuCourse.getSmallLandscape() 2");
            return "1024x768_" + this.getLandscapeImage();
        } else {
            System.out.println("ImenuCourse.getSmallLandscape() 3");
            return null;
        }
    }

    public String getHugePortrait() {
        if (this.isHasPortraitImage()) {
            return "768x1024_" + this.getPortraitImage();
        } else {
            return null;
        }
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getStar() {
        return star;
    }

    public void setStar(Integer star) {
        this.star = star;
    }

    public String getRecipe() {
        return recipe;
    }

    public void setRecipe(String recipe) {
        this.recipe = recipe;
    }

    public Integer getMeasure() {
        return measure;
    }

    public void setMeasure(Integer measure) {
        this.measure = measure;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getPortraitImage() {
        return portraitImage;
    }

    public void setPortraitImage(String portraitImage) {
        this.portraitImage = portraitImage;
    }

    public String getLandscapeImage() {
        return landscapeImage;
    }

    public void setLandscapeImage(String landscapeImage) {
        this.landscapeImage = landscapeImage;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public Integer getTagId() {
        if(tag != null){
            return tag.getId();
        }
        return tagId;
    }

    public void setTagId(Integer tagId) {
        this.tagId = tagId;
    }

    public ImenuTag getTag() {
        return tag;
    }

    public void setTag(ImenuTag tag) {
        this.tag = tag;
    }

    public ImenuMenu getMenu() {
        return menu;
    }

    public void setMenu(ImenuMenu menu) {
        this.menu = menu;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ImenuCourse)) {
            return false;
        }
        ImenuCourse other = (ImenuCourse) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.imenu.entity.ImenuCourse[ id=" + id + " ]";
    }
}
